package com.zte.user.control;

import com.zte.user.model.Student;
import com.zte.user.model.Teacher;
import com.zte.user.model.Users;
import com.zte.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserAction {
    @Autowired
    private IUserService service;

    @RequestMapping("/login")
    @ResponseBody
    public List<Users> login(Users users){
        System.out.println(users);
        System.out.println(1);
        List<Users> users1 = service.login(users);

        return users1;
    }
    @RequestMapping("/register")
    @ResponseBody
    public boolean register(String role,String username,String password,String gender,String clazz){
        boolean b = false;
        boolean b1 = false;
        boolean b2 = false;

        Users users = new Users();
        users.setUsername(username);
        if(role.equals("1")){
            Student student=new Student("3",username,gender,clazz,password);
            Users user=new Users(username,password,role);
            users.setRole(role);
            List<Users> all = service.findAll(users);
            if(all.size()>0){
                return b;
            }else {
                b1 = service.addStu(student);
                b2 = service.addUser(user);
                System.out.println(b1);
                System.out.println(b2);
                if (b1 == true && b2 == true) {
                    b = true;
                }
            }
        }if(role.equals("2")){
            Teacher teacher=new Teacher("1",username,gender,password);
            Users user=new Users(username,password,role);
            users.setRole(role);
            List<Users> all = service.findAll(users);
            if(all.size()>0){
                return b;
            }else {
                b1 = service.addTeacher(teacher);
                b2 = service.addUser(user);
                System.out.println(b1);
                System.out.println(b2);
                if (b1 == true && b2 == true) {
                    b = true;
                }
            }
        }
        return b;
    }
}
